package top.mataiji.manager.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import mataiji.config.util.Common;
import top.mataiji.manager.mapper.BookMapper;
import top.mataiji.manager.mapper.OrderMapper;
import top.mataiji.manager.mapper.Order_detailsMapper;
import top.mataiji.manager.pojo.*;
import top.mataiji.manager.pojo.vo.OrderVo;
import top.mataiji.manager.pojo.dtd.OrderDtd;
import top.mataiji.manager.service.OrderService;
import top.mataiji.manager.service.Order_detailsService;

import java.util.*;

/**
 * @Auther: zhaoss
 * @Date: 2021/3/19 - 03 - 19 - 14:40
 * @Description: top.mataiji.shop.service.impl
 * @version: 1.0
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    Order_detailsService order_detailsService;
    @Autowired
    BookMapper bookMapper;

    @Autowired
    Order_detailsMapper  order_detailsMapper;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addOrder(OrderVo orderVo) throws Exception{
        Order order = orderVo.getOrder();
        //获取商品详情
        List<Book> books = orderVo.getBook();
        //获取订单号
        String orderId = order.getOrderId();
        //创建订单详情集合
        ArrayList<Order_details> list = new ArrayList<>();
        Iterator<Book> iterator = books.iterator();
        //根据参数加入订单详情
        while(iterator.hasNext()){
            Book book = iterator.next();
            Order_details order_details = new Order_details();
            order_details.setOrderId(orderId);
            order_details.setBookId(book.getBookid().longValue());
            list.add(order_details);
        }
        //往订单详情插入数据
        int i1 = order_detailsService.addOrder_details(list);
        if(i1==0){
            try {

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        System.out.println(order.getOrderId());
        order.setOrderId(orderId);
        int i=0;
        try {

            i= orderMapper.insertSelective(order);
        }catch (Exception e){
            e.printStackTrace();
        }
        return i;
    }

    @Transactional(rollbackFor = Exception.class)
    public String addOrder(OrderDtd orderDtd) throws Exception{
        OrderVo orderVo = new OrderVo();
        Order order = new Order();
        order.setOrderStatus(0L);
        //获取orderId;
        String orderId = Common.getOrderId()+orderDtd.getUserId();
        order.setOrderId(orderId);
        order.setOrderMoney(orderDtd.getOrderPrice());
        order.setUpdateDate(new Date());
        order.setEstablishDate(new Date());
        order.setUserid(orderDtd.getUserId());
        //设置订单详情页面
        orderVo.setOrder(order);
        //设置全部书籍
        List<Integer> bookids = orderDtd.getBookid();
        List<Book> books = new ArrayList<>();
        //查询所有书籍
        for (Integer bookid : bookids) {
            Book book = bookMapper.selectByPrimaryKey(bookid);
            books.add(book);
        }
        orderVo.setBook(books);
        int i  = addOrder(orderVo);
        if (i!=0){
            return orderId;
        }
        return null;
    }

    /**
     * 查询所有订单
     * @return
     */
    @Override
    public List<OrderVo> findAllOrder(Integer userId) {
        //创建查询对象
        OrderExample orderExample = new OrderExample();
        //设置查询参数
        orderExample.createCriteria().andUseridEqualTo(userId);
        List<Order> orders = orderMapper.selectByExample(orderExample);
        ListIterator<Order> orderListIterator = orders.listIterator();
        //创建一个booklist
        List<Book> books = new ArrayList<>();
        //创建OrderVo对象
        OrderVo orderVo ;
        //创建返回对象
        List<OrderVo> orderVos = new ArrayList<>();
        while (orderListIterator.hasNext()){
            orderVo = new OrderVo();
            Order order = orderListIterator.next();
            String orderId = order.getOrderId();
            orderVo.setOrder(order);
            //查询数据id
            //创建查询对象
            Order_detailsExample order_detailsExample = new Order_detailsExample();
            order_detailsExample.createCriteria().andOrderIdEqualTo(orderId);
            List<Order_details> order_details = order_detailsMapper.selectByExample(order_detailsExample);
            //查询所有书籍
            Iterator<Order_details> iterator = order_details.iterator();
            while (iterator.hasNext()){
                Order_details next = iterator.next();
                //查询书籍
                BookExample bookExample = new BookExample();
                //设置查询参数
                bookExample.createCriteria().andBookidEqualTo(next.getBookId().intValue());
                System.out.println(bookMapper.selectByExample(bookExample));
                for (Book book : bookMapper.selectByExample(bookExample)) {
                    books.add(book);
                }

            }
            orderVo.setBook(books);
            orderVos.add(orderVo);

        }

        return orderVos;
    }

    @Override
    public Integer alertOrderStatus(String orderId) {
        Order order = new Order();
        order.setOrderStatus(1L);
        order.setUpdateDate(new Date());
        OrderExample orderExample = new OrderExample();
        orderExample.createCriteria().andOrderIdEqualTo(orderId);
        return orderMapper.updateByExampleSelective(order,orderExample);
    }
}
